package com.v1.video.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.SecondEntity.SecondInfo;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.TransferUtils;
import com.v1.video.util.Utils;
import com.v1.video.view.danmaku.DanmakuHelper;

import java.util.ArrayList;
import java.util.List;

public class SecondAdapter2 extends BaseAdapter {
	
	private Context mContext;
	private List<SecondInfo> mLstData;
	private int mHeight;
	private int mCurIndex = -1;
	private DanmakuHelper mDanmakuHelper;

	public SecondAdapter2(Context context) {
		mContext = context;
		mLstData = new ArrayList<SecondInfo>();
		mHeight = Utils.computeImageHeight(mContext);
	}

	public List<SecondInfo> getLstData() {
		return mLstData;
	}
	
	public void clearLstData() {
		if (mLstData != null) {
			this.mLstData.clear();
		}
	}

	public void setLstData(List<SecondInfo> lstData) {
		if (lstData != null && lstData.size() > 0) {
			this.mLstData.addAll(lstData);
			this.notifyDataSetChanged();
		}
	}

	
	public int getCurIndex() {
		return mCurIndex;
	}

	public void setCurIndex(int curIndex) {
		this.mCurIndex = curIndex;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.fragment_second_item2,
					null);
			holder = new Holder();
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.iv_headimg = (ImageView) convertView.findViewById(R.id.iv_headimg);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
			holder.tv_guanzhu = (TextView) convertView.findViewById(R.id.tv_guanzhu);
			holder.tv_canyu = (TextView) convertView.findViewById(R.id.tv_canyu);
			holder.tv_share = (TextView) convertView.findViewById(R.id.tv_share);
			holder.danmaku = (LinearLayout) convertView
					.findViewById(R.id.lay_danmaku);
			holder.lay_view = (FrameLayout) convertView
					.findViewById(R.id.lay_view);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final SecondInfo info = mLstData.get(position);
		ViewGroup.LayoutParams params = holder.lay_view.getLayoutParams();
		params.height = mHeight;
		holder.lay_view.setLayoutParams(params);
		String text = "";
		if (!TextUtils.isEmpty(info.getMark())) {
			text = "[" + info.getMark() + "]";
		}
		if (!TextUtils.isEmpty(info.getTitle())) {
			text = text + info.getTitle();
		}
		holder.title.setText(text);
		if (!TextUtils.isEmpty(info.getPlayCount())) {
			holder.tv_guanzhu.setText(info.getPlayCount() + "人关注");
		} else {
			holder.tv_guanzhu.setText("0人关注");
		}
		
		if (!TextUtils.isEmpty(info.getForwordCount())) {
			holder.tv_canyu.setText(info.getForwordCount() + "人参与");
		} else {
			holder.tv_canyu.setText("0人参与");
		}
		
		if (!TextUtils.isEmpty(info.getPariseCount())) {
			holder.tv_share.setText(info.getPariseCount() + "次分享");
		} else {
			holder.tv_share.setText("0次分享");
		}
		
		if (!TextUtils.isEmpty(info.getNickname())) {
			holder.tv_username.setText(info.getNickname());
		} else {
			holder.tv_username.setText("");
		}
		
		if (!TextUtils.isEmpty(info.getDesc())) {
			holder.tv_desc.setText(info.getDesc());
		} else {
			holder.tv_desc.setText("");
		}
		
		ImageLoader.getInstance().displayImage(info.getAvatar(),
				holder.iv_headimg, Constant.IMAGE_OPTIONS_FOR_USER_OTHER, new SimpleImageLoadingListener() {

			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				if(loadedImage!=null){
					ImageView iv = (ImageView) view;
					iv.setImageBitmap(Utils.toRoundBitmap(loadedImage));
				}
			}

		});
		
		if (!TextUtils.isEmpty(info.getPic())) {
			ImageLoader.getInstance().displayImage(info.getPic(), holder.image,
					Constant.VIDEO_10_16_OPTION);
		} else {
			holder.image
					.setImageResource(R.drawable.common_place_holder_bg_16_10);
		}
		
//		if (mCurIndex != -1 && mCurIndex == position
//				&& info.getCommentList() != null
//				&& info.getCommentList().size() > 0) {
//			holder.danmaku.setVisibility(View.VISIBLE);
//			mDanmakuHelper = new DanmakuHelper(mContext, holder.danmaku);
//			mDanmakuHelper.addItem(info.getCommentList());
//			mDanmakuHelper.start();
//		} else {
//			holder.danmaku.setVisibility(View.GONE);
//
//		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ParamList param = new ParamList();
				param.add(new Parameter("devid",Constant.DEVICEID));
				param.add(new Parameter("version",Constant.PRODUCT_VERSION));
				param.add(new Parameter("pcode",Constant.PRODUCT_CODE));
				param.add(new Parameter("uid",LoginInfo.getInstance().getUserId()));
				param.add(new Parameter("vid",info.getVid()));
				RequestManager.getInstance().postRequest(mContext,Constant.SECOND_ADD_PLAYNUM, param, "second_addplaynum", new OnResponseListener(){

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						
					}
					
				});
				
				TransferUtils.getInstance().transferSecondVideoDetail(mContext,info.getId());
				String forwordCount_str = info.getForwordCount();
				int forwordCount = Integer.valueOf(forwordCount_str);
				forwordCount = forwordCount+1;
				info.setForwordCount(String.valueOf(forwordCount));
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
	
	public void release() {
//		if (mDanmakuHelper != null) {
//			mDanmakuHelper.stop();
//			mDanmakuHelper.removeAllViews();
//			mDanmakuHelper = null;
//		}
		mCurIndex = -1;
	}
	
	private class Holder{
		public ImageView image;
		public TextView title;
		public TextView playnum;
		public LinearLayout danmaku;
		public FrameLayout lay_view;
		public ImageView iv_headimg;
		public TextView tv_username,tv_guanzhu,tv_canyu,tv_share,tv_desc;
	}

}
